package com.devfest.wishmaster.model;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable
public class WishItem {

	@DatabaseField(id = true)
	private int id;

	@DatabaseField(dataType = DataType.DATE, canBeNull = true)
	private Date dateCreated;

	@DatabaseField
	private String description;

	@DatabaseField
	private String image;

	@DatabaseField
	private double price;

	@DatabaseField
	private String title;

	private List<String> images;

	@DatabaseField(foreign = true, foreignAutoRefresh = true)
	private WishList wishList;

	@ForeignCollectionField
	private ForeignCollection<Comment> commentsDb;

	private List<Comment> comments;

	private List<Comment> newComments;

	public List<Comment> getComments() {
		if (comments == null)
			comments = new ArrayList<Comment>();
		return comments;
	}

	public List<Comment> getNewComments() {
		if (newComments == null)
			newComments = new ArrayList<Comment>();
		return newComments;
	}

	@DatabaseField
	private int mode;

	/**
	 * @return the mode
	 */
	public int getMode() {
		return mode;
	}

	/**
	 * @param mode
	 *            the mode to set
	 */
	public void setMode(int mode) {
		this.mode = mode;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the dateCreated
	 */
	public Date getDateCreated() {
		return dateCreated;
	}

	/**
	 * @param dateCreated
	 *            the dateCreated to set
	 */
	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	/**
	 * @param dateCreated
	 *            the dateCreated to set
	 */
	@SuppressLint("SimpleDateFormat")
	public void setDateCreated(String dateCreated) {
		if (dateCreated.equals("")) {
			this.dateCreated = new Date(0);
			return;
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		while (!dateCreated.endsWith("00")) {
			dateCreated += "0";
		}
		try {
			this.dateCreated = formatter.parse(dateCreated.trim());
		} catch (ParseException e) {
		}
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the image
	 */
	public Bitmap getImage() {
		if (image != null && !image.equals("")) {
			byte[] decodedByte = Base64.decode(image, Base64.DEFAULT);
			return BitmapFactory.decodeByteArray(decodedByte, 0,
					decodedByte.length, null);
		}
		return null;
	}

	/**
	 * @param image
	 *            the image to set
	 */
	public void setImage(String image) {
		InputStream is;
		HttpURLConnection conn;
		if (image.equals(""))
			return;
		try {
			URL url = new URL(image);
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.connect();
			is = conn.getInputStream();
			Bitmap bm = BitmapFactory.decodeStream(is);

			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
			byte[] b = baos.toByteArray();
			this.image = Base64.encodeToString(b, Base64.DEFAULT);
		} catch (MalformedURLException e) {
		} catch (IOException e) {
		}
	}

	/**
	 * @return the price
	 */
	public double getPrice() {
		return price;
	}

	/**
	 * @param price
	 *            the price to set
	 */
	public void setPrice(double price) {
		this.price = price;
	}

	/**
	 * @return the wishList
	 */
	public WishList getWishList() {
		return wishList;
	}

	/**
	 * @param wishList
	 *            the wishList to set
	 */
	public void setWishList(WishList wishList) {
		this.wishList = wishList;
	}

	/**
	 * @return the comments
	 */
	public ForeignCollection<Comment> getCommentsDb() {
		return commentsDb;
	}

	public List<String> getImages() {
		return images;
	}

	public void setImages(List<String> images) {
		this.images = images;
	}

	public WishItem() {
	}
}